Added tracking to POJO returned when assigning new data #398
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I was running into an issue (possibly related to #384) where native getters on my controllers would not recompute their values when the Apollo cache was updated. This became especially apparent when using an API that took advantage of relay-style pagination: a computed property that pulled out all the edges' nodes on a field would not update when either the node was changed or a new edge was added (e.g., as part of a create mutation). What I found particularly odd was that I could make the update work if I added a
@computed
decorator to the getter. So, for example:Because I could get it to work using the decorator, it seemed like there was something that wasn't getting set up for autotracking using
@tracked
when the data was initially assigned, resulting insetProperties()
doing nothing until something registered that it was watching the value via the classic observation system.Thankfully, the solution seems, at least on the surface, to be pretty simple: we can utilize tracked built-ins to have our initial POJO actually be a
TrackedObject
, which hooks it into the modern@tracked
autotracking system. This way, even a getter on a Controller will register that it consumes the data, and a refetch will trigger an update. 🎉(When attempting to write the test, my first attempt utilized a Component to pull the reviews out of a movie and display the star ratings; however, assigning the movie as an argument to a Component happened to register it with the autotracking system (I'm guessing), since Component arguments are automatically auto-tracked. The tests passed before any changes were made to the rest of the code! 😅 This would mean that for users who only pass fetched data directly down to a Component before transforming/consuming, they might not ever encounter the issue this PR is attempting to address, since it would appear to "just work" in that case.)
Thanks for your feedback and consideration! Fantastic (and indispensable) addon! 😁